set :application, 'minerva'
set :base_repository, "http://minerva-dev.googlecode.com/svn"
if variables[:tag]
  set :repository, "#{base_repository}/tags/#{variables[:tag]}"
elsif variables[:branch]
  set :repository, "#{base_repository}/branches/#{variables[:branch]}"
else
  set :repository, "#{base_repository}/trunk"
end

set :scm_username, 'seairy'

task :production do
  role :app, '60.247.115.253:3030'
  role :web, '60.247.115.253:3030'
  role :db, '60.247.115.253:3030', :primary => true

  set :user, 'seairy'
  set :password, 'th3b3s0f3!'
  set :deploy_via, :remote_cache
  set :copy_compression, :bzip2
  set :deploy_to, "/srv/www/minerva"
  set :rails_env, 'development'

  task :after_update_code, :roles => :app do
    run "rm #{release_path}/config/deploy.rb"
    run "rm #{release_path}/Capfile"
    run "ln -s #{shared_path}/database/database.yml #{release_path}/config/database.yml"
    run "ln -s #{shared_path}/assets #{release_path}/public/assets"
  end

  namespace :deploy do
    task :start, :roles => :app do
      26000.upto 26009 do |port|
        run "/usr/local/ruby/bin/mongrel_rails start -d -e production -a 127.0.0.1 -c #{current_path} -p #{port} -P tmp/pids/mongrel.#{port}.pid"
      end
    end
    task :stop, :roles => :app do
      26000.upto 26009 do |port|
        run "/usr/local/ruby/bin/mongrel_rails stop -c #{current_path} -P #{current_path}/tmp/pids/mongrel.#{port}.pid"
      end
    end
    task :restart, :roles => :app do
      deploy.stop
      deploy.start
    end
  end
end

task :development do
  role :app, '60.247.115.160:3030'
  role :web, '60.247.115.160:3030'
  role :db, '60.247.115.160:3030', :primary => true

  set :user, 'root'
  set :password, 'th3b3s0f3!'
  set :sudo, true
  set :deploy_via, :remote_cache
  set :copy_compression, :bzip2
  set :deploy_to, "/srv/www/minerva"
  set :rails_env, 'production'

  task :after_update_code, :roles => :app do
    run "rm #{release_path}/config/deploy.rb"
    run "rm #{release_path}/Capfile"
    run "ln -s #{shared_path}/database/database.yml #{release_path}/config/database.yml"
    run "ln -s #{shared_path}/assets #{release_path}/public/assets"
  end

  namespace :deploy do
    task :start, :roles => :app do
      run "/usr/local/ruby/bin/mongrel_rails start -c #{current_path} -p 80 -d"
    end

    task :stop, :roles => :app do
      run "/usr/local/ruby/bin/mongrel_rails stop -c #{current_path}"
    end

    task :restart, :roles => :app do
      deploy.stop
      deploy.start
    end
  end
end

# Utility tasks
desc 'Check uptime'
task :uptime, :roles => :app do
  run 'uptime'
end

desc 'Check uname'
task :uname, :roles => :app do
  run 'uname -srp'
end

desc 'Grep for ruby processes'
task :grep_ruby, :roles => :app do
  run 'ps -ef | grep ruby | grep -v grep'
end

desc 'tail log files'
task :tail_logs, :roles => :app do
  log_file = ARGV[1] ? ARGV[1] : rails_env
  run "tail -f #{shared_path}/log/#{log_file}.log" do |channel, stream, data|
    puts  # for an extra line break before the host name
    puts "#{channel[:host]}: #{data}"
    break if stream == :err
  end
end

desc 'Check revision of running project'
task :show_revision, :roles => :app do
  run "cat #{current_path}/REVISION"
end